package com.spring.study.bean.definition;

import com.spring.study.bean.factory.UserFactory;
import com.spring.study.bean.factory.UserFactoryImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.PostConstruct;

/**
 * bean的初始化
 */
@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(BeanInitializationDemo.class);
        applicationContext.refresh();
        //非延迟初始化在spring应用上下文启动完成后，被初始化
        System.out.println("spring应用上下文启动开始》》》");
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);
        System.out.println(userFactory);
        System.out.println("spring应用上下文准备关闭》》》");
        applicationContext.close();
        System.out.println("spring应用上下文关闭》》》");
    }

    @Bean(initMethod = "initUserFactory",destroyMethod = "doDestroy")
    //延迟加载
    @Lazy(false)
    public UserFactory userFactory() {
        return new UserFactoryImpl();
    }

    @Override
    public void finalize() throws Throwable {
        System.out.println("当前对象正在被垃圾回收。。。。。");
    }

}
